package org.dftproject.genesis.ui.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

public class ButtonPainter {
	
	public static final int NONE = 0;
	public static final int HOVER = 1;
	public static final int SELECTED = 2;
	
	private final Color hoverBorderColor = new Color(null, 0x55, 0x55, 0x55);
	private final Color borderColor = new Color(null, 0x99, 0x99, 0x99);
	
	private final Color hoverTopTop = new Color(null, 255, 255, 255);
	private final Color hoverTopBottom = new Color(null, 255, 231, 144);
	private final Color hoverBottomTop = new Color(null, 255, 215, 76);
	private final Color hoverBottomBottom = new Color(null, 255, 231, 144);

	private final Color selectedTopTop = new Color(null, 255, 255, 255);
	private final Color selectedTopBottom = new Color(null, 234, 244, 255);
	private final Color selectedBottomTop = new Color(null, 196, 225, 255);
	private final Color selectedBottomBottom = new Color(null, 234, 244, 255);
	
	private final Color normalTopTop = new Color(null, 244, 244, 244);
	private final Color normalTopBottom = new Color(null, 225, 225, 225);
	private final Color normalBottomTop = new Color(null, 206, 206, 206);
	private final Color normalBottomBottom = new Color(null, 244, 244, 244);
	
	public void drawButton(Graphics graphics, int state, Rectangle bounds) {
		if ((state & SELECTED) != 0 || (state & HOVER) != 0) {
			Rectangle innerBounds = bounds.getCropped(new Insets(1));
			
			if ((state & HOVER) != 0) {
				graphics.setForegroundColor(hoverTopTop);
				graphics.setBackgroundColor(hoverTopBottom);
				graphics.fillGradient(innerBounds.getResized(0, -innerBounds.height / 2), true);
				
				graphics.setForegroundColor(hoverBottomTop);
				graphics.setBackgroundColor(hoverBottomBottom);
				graphics.fillGradient(innerBounds.getResized(0, -innerBounds.height / 2).getTranslated(0, innerBounds.height / 2), true);
			} else if ((state & SELECTED) != 0) {
				graphics.setForegroundColor(selectedTopTop);
				graphics.setBackgroundColor(selectedTopBottom);
				graphics.fillGradient(innerBounds.getResized(0, -innerBounds.height / 2), true);
				
				graphics.setForegroundColor(selectedBottomTop);
				graphics.setBackgroundColor(selectedBottomBottom);
				graphics.fillGradient(innerBounds.getResized(0, -innerBounds.height / 2).getTranslated(0, innerBounds.height / 2), true);
			} else {
				graphics.setForegroundColor(normalTopTop);
				graphics.setBackgroundColor(normalTopBottom);
				graphics.fillGradient(innerBounds.getResized(0, -innerBounds.height / 2), true);
				
				graphics.setForegroundColor(normalBottomTop);
				graphics.setBackgroundColor(normalBottomBottom);
				graphics.fillGradient(innerBounds.getResized(0, -innerBounds.height / 2).getTranslated(0, innerBounds.height / 2), true);
			}
			
			graphics.setAntialias(SWT.ON);
			graphics.setForegroundColor(ColorConstants.white);
			graphics.setLineWidth(2);
			graphics.drawRoundRectangle(bounds.getCropped(new Insets(1)), 6, 6);
			graphics.setForegroundColor((state & HOVER) != 0 ? hoverBorderColor : borderColor);
			graphics.setLineWidth(1);
			graphics.drawRoundRectangle(bounds.getResized(-1, -1), 6, 6);
			graphics.setAntialias(SWT.OFF);
		}
	}

}
